package com.carey.blog.framework.latke;

import java.util.Locale;

import com.carey.blog.framework.latke.util.Strings;

/**
 * Latke framework configuration utility facade.
 * 
 * <p>
 * If the application runs on {@linkplain RunsOnEnv#LOCAL local environment},
 * please set {@linkplain #repositoryPath} before setting up your application.
 * </p>
 * 
 */
public final class Latkes {

	/**
	 * Default locale. Initializes this by
	 * {@link #setDefaultLocale(java.util.Locale)}.
	 */
	private static Locale defaultLocale;
	/**
	 * Local repository path.
	 */
	private static String repositoryPath;
	/**
	 * Where latke runs on?.
	 */
	private static RunsOnEnv runsOnEnv;

	/**
	 * Checks environment settings.
	 */
	public static void check() {
		final RunsOnEnv runsOn = getRunsOnEnv();
		if (runsOn.equals(RunsOnEnv.LOCAL)) {
			getRepositoryPath();
		}
	}

	/**
	 * Sets the runs on with the specified environment.
	 * 
	 * @param runsOnEnv
	 *            the specified environment
	 */
	public static void setRunsOnEnv(final RunsOnEnv runsOnEnv) {
		Latkes.runsOnEnv = runsOnEnv;
	}

	/**
	 * Gets the runs on environment.
	 * 
	 * @return runs on environment
	 */
	public static RunsOnEnv getRunsOnEnv() {
		if (null == Latkes.runsOnEnv) {
			throw new RuntimeException(
					"Runs on enviornment has not been initialized!");
		}

		return Latkes.runsOnEnv;
	}

	/**
	 * Sets the repository path with the specified repository path.
	 * 
	 * @param repositoryPath
	 *            the specified repository path
	 */
	public static void setRepositoryPath(final String repositoryPath) {
		Latkes.repositoryPath = repositoryPath;
	}

	/**
	 * Gets the repository path.
	 * 
	 * @return repository path
	 */
	public static String getRepositoryPath() {
		if (Strings.isEmptyOrNull(repositoryPath)) {
			throw new RuntimeException(
					"Repository path has not been initialized!");
		}

		return repositoryPath;
	}

	/**
	 * Sets the default locale.
	 * 
	 * @param locale
	 *            a new default locale
	 */
	public static void setDefaultLocale(final Locale locale) {
		defaultLocale = locale;
	}

	/**
	 * Gets the default locale. If the {@link #defaultLocale} has not been
	 * initialized, invoking this method will throw {@link RuntimeException}.
	 * 
	 * @return the default locale
	 */
	public static Locale getDefaultLocale() {
		if (null == defaultLocale) {
			throw new RuntimeException(
					"Default locale has not been initialized!");
		}

		return defaultLocale;
	}

	/**
	 * Private constructor.
	 */
	private Latkes() {
	}
}
